package com.hncc.kumao.util;

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.struts2.util.FastByteArrayOutputStream;
import org.apache.tools.ant.Task;

import com.alibaba.fastjson.JSON;
import com.hncc.kumao.entity.BreakpointUploadTaskInfo;
import com.hncc.kumao.entity.OfflineDownloadTaskInfo;

import freemarker.core.ReturnInstruction.Return;
import redis.clients.jedis.Jedis;

public class BreakpointUploadUtils {

    //获取当前正在上传的文件片段序号
    public static List<Integer> getCurrentFragment(String taskId) {
        Jedis jedis = JedisUtils.getJedis();
        String jsonString = jedis.hget(ConstValue.BREAKPOINT_UPLOAD_CURRENT_FRAGMENT, taskId);
        JedisUtils.closeJedis(jedis);
        if(jsonString == null || jsonString.trim().length() <= 0) {
            return new ArrayList<Integer>();
        } else {
            List<Integer> list = JSON.parseArray(jsonString, Integer.class);
            return list;
        }
    }
    
    //设置当前正在上传的文件片段
    public static void setCurrentFragment(String taskId, List<Integer> list) {
        String jsonString = JSON.toJSONString(list);
        Jedis jedis = JedisUtils.getJedis();
        jedis.hset(ConstValue.BREAKPOINT_UPLOAD_CURRENT_FRAGMENT, taskId, jsonString);
        JedisUtils.closeJedis(jedis);
    }
    
    //添加一个当前正在上传的文件片段序号
    public static void addCurrentFragment(String taskId, Integer number) {
        List<Integer> list = getCurrentFragment(taskId);
        boolean flag = true;
        for (Integer integer : list) {
            if(integer == number) {
                //该片段之前已记录过
                flag = false;
                break;
            }
        }
        if(flag) {
            list.add(number);            
        }
        setCurrentFragment(taskId, list);
    }
    
    //移除一个当前正在上传的文件片段序号
    public static void removeCurrentFragment(String taskId, Integer number) {
        List<Integer> list = getCurrentFragment(taskId);
        Iterator<Integer> iterator = list.iterator();
        while (iterator.hasNext()) {
            Integer n = iterator.next();
            if (n == number) {
                iterator.remove();//使用迭代器的删除方法删除
            }
        }
        setCurrentFragment(taskId, list);
    }
    
    //删除所有正在上传的文件片段序号
    public static void removeAllCurrentFragment(String taskId) {
        Jedis jedis = JedisUtils.getJedis();
        jedis.hdel(ConstValue.BREAKPOINT_UPLOAD_CURRENT_FRAGMENT, taskId);
        JedisUtils.closeJedis(jedis);
    }
    
    /********************************************************/
    
    //获取上传完成的文件片段序号
    public static List<Integer> getSuccessFragment(String taskId) {
        Jedis jedis = JedisUtils.getJedis();
        String jsonString = jedis.hget(ConstValue.BREAKPOINT_UPLOAD_SUCCESS_FRAGMENT, taskId);
        JedisUtils.closeJedis(jedis);
        if(jsonString == null || jsonString.trim().length() <= 0) {
            return new ArrayList<Integer>();
        } else {
            List<Integer> list = JSON.parseArray(jsonString, Integer.class);
            return list;
        }
    }
    
    //获取上传完成的文件片段数量
    public static Integer getSuccessFragmentNumber(String taskId) {
        return getSuccessFragment(taskId).size();
    }
    
    //获取上传完成的文件片段序号排序后的数组
    public static Integer[] getSuccesssFragmentSortArray(String taskId) {
        //将list转成数组
        List<Integer> list = BreakpointUploadUtils.getSuccessFragment(taskId);
        Integer[] array = new Integer[list.size()];
        list.toArray(array);
        //冒泡排序
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array.length - i -1; j++) {   // 这里说明为什么需要-1
                if (array[j] > array[j + 1]) {
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                }
            }
        }
        return array;
    }
    
    //获取接收完成的序号是否连贯
    public static boolean getSuccessFragmentNumberIsCoherent(String taskId) {
        Integer[] arr = getSuccesssFragmentSortArray(taskId);
        for(int i = 0; i < arr.length; i++) {
            if(i != arr.length - 1) {
                if(arr[i] + 1 != arr[i + 1]) {
                    return false;
                }
            }
        }
        return true;
    }
    
    //获取接收完成的序号中连贯的最大的
    public static Integer getSuccessFragmentNumberMaxCoherent(String taskId) {
        Integer[] arr = getSuccesssFragmentSortArray(taskId);
        if(arr.length > 0) {
            for(int i = 0; i < arr.length; i++) {
                if(i != arr.length - 1) {
                    if(arr[i] + 1 != arr[i + 1]) {
                        return arr[i];
                    }
                }
            }
            return arr[arr.length - 1];
        } else {
            return 0;
        }
        
    }
    
    //设置上传完成的文件片段
    public static void setSuccessFragment(String taskId, List<Integer> list) {
        String jsonString = JSON.toJSONString(list);
        Jedis jedis = JedisUtils.getJedis();
        jedis.hset(ConstValue.BREAKPOINT_UPLOAD_SUCCESS_FRAGMENT, taskId, jsonString);
        JedisUtils.closeJedis(jedis);
    }
    
    //添加一个上传完成的文件片段序号
    public static void addSuccessFragment(String taskId, Integer number) {
        List<Integer> list = getSuccessFragment(taskId);
        boolean flag = true;
        for (Integer integer : list) {
            if(integer == number) {
                //该片段之前已记录过
                flag = false;
                break;
            }
        }
        if(flag) {
            list.add(number);            
        }
        setSuccessFragment(taskId, list);
    }
    
    //移除一个上传完成的文件片段序号
    public static void removeSuccessFragment(String taskId, Integer number) {
        List<Integer> list = getSuccessFragment(taskId);
        Iterator<Integer> iterator = list.iterator();
        while (iterator.hasNext()) {
            Integer n = iterator.next();
            if (n == number) {
                iterator.remove();//使用迭代器的删除方法删除
            }
        }
        setSuccessFragment(taskId, list);
    }
    
    //删除所有上传完成的文件片段序号
    public static void removeAllSuccessFragment(String taskId) {
        Jedis jedis = JedisUtils.getJedis();
        jedis.hdel(ConstValue.BREAKPOINT_UPLOAD_SUCCESS_FRAGMENT, taskId);
        JedisUtils.closeJedis(jedis);
    }
    
    /********************************************************/
    
    //获取断点上传信息
    public static BreakpointUploadTaskInfo getBreakpointUploadTaskInfo(String taskId) {
        Jedis jedis = JedisUtils.getJedis();
        String jsonString = jedis.hget(ConstValue.BREAKPOINT_UPLOAD_TASK_INFO, taskId);
        JedisUtils.closeJedis(jedis);
        BreakpointUploadTaskInfo taskInfo;
        if(jsonString == null || jsonString.trim().length() <= 0) {
            taskInfo = new BreakpointUploadTaskInfo();
            taskInfo.setTaskId("0");
            taskInfo.setSuccessFragmentNumber(0);
            taskInfo.setTotalFragmentNumber(0);
            taskInfo.setState(0);
        } else {
            taskInfo = JSON.parseObject(jsonString, BreakpointUploadTaskInfo.class);
        }
        return taskInfo;
    }
    //获取离线下载任务进度
    public static String getBreakpointUploadTaskInfoJson(String taskId) {
        Jedis jedis = JedisUtils.getJedis();
        String jsonString = jedis.hget(ConstValue.BREAKPOINT_UPLOAD_TASK_INFO, taskId);
        JedisUtils.closeJedis(jedis);
        return jsonString;
    }
    //设置断点上传信息
    public static void setBreakpointUploadTaskInfo(BreakpointUploadTaskInfo taskInfo) {
        Map map = new HashMap();
        map.put("taskId", taskInfo.getTaskId());
        map.put("successFragmentNumber", taskInfo.getSuccessFragmentNumber());
        map.put("totalFragmentNumber", taskInfo.getTotalFragmentNumber());
        map.put("state", taskInfo.getState());
        map.put("createTime", taskInfo.getCreateTime());
        map.put("startGetTime", taskInfo.getStartGetTime());
        map.put("endGetTime", taskInfo.getEndGetTime());
        map.put("startUploadTime", taskInfo.getStartUploadTime());
        map.put("endUploadTime", taskInfo.getEndUploadTime());
        String jsonString = JSON.toJSONString(map);
        Jedis jedis = JedisUtils.getJedis();
        jedis.hset(ConstValue.BREAKPOINT_UPLOAD_TASK_INFO, taskInfo.getTaskId(), jsonString);
        JedisUtils.closeJedis(jedis);
    }
    //删除断点上传信息
    public static void removeBreakpointUploadTaskInfo(String taskId) {
        Jedis jedis = JedisUtils.getJedis();
        jedis.hdel(ConstValue.BREAKPOINT_UPLOAD_TASK_INFO, taskId);
        JedisUtils.closeJedis(jedis);
    }
    //获取断点上传任务状态
    public static Integer getBreakpointUploadTaskInfoState(String taskId) {
        return getBreakpointUploadTaskInfo(taskId).getState();
    }
}
